package com.ywu.common.result;

import com.ywu.common.constant.CommonConstant;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * @ClassName CommonResult
 * @Description API层统一响应结果
 * @Author GroundDemo
 * @Date 2023/11/12 19:05
 * @Version 1.0
 **/
@Getter
@Setter
@ToString
@Accessors(chain = true)
public class CommonResult<T> {
    private String code;

    private T data;

    private String errorCode;

    private String message;

    private CommonResult(){}

    private CommonResult(String code, T data, String errorCode, String message){
        this.code = code;
        this.data = data;
        this.errorCode = errorCode;
        this.message = message;
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(CommonConstant.SUCCESS_CODE, data, null, CommonConstant.SUCCESS_MESSAGE);
    }

    public static <T> CommonResult<T> error() {
        return new CommonResult<T>(CommonConstant.ERROR_CODE, null, null,
                CommonConstant.ERROR_MESSAGE);
    }

    public static <T> CommonResult<T> common() {
        return new CommonResult<T>();
    }
}
